/*
 * Project Name: jmax
 * File Name: ParkDeviceRestful.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.parking.protocol.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.common.utils.JsonUtil;
import com.xdtech.common.utils.RequestUtil;
import com.xdtech.parking.protocol.util.ParkRedis;
import com.xdtech.parking.protocol.vo.DeviceInfo;
import com.xdtech.parking.protocol.vo.ParkDeviceInfo;
import com.xdtech.parking.service.ParkingDeviceService;
import com.xdtech.web.util.ResponseUtil;

/**
 * 设备通信 restful接口
 * 
 * @author max.zheng
 * @version TODO 添加版本
 * @see 相关类或方法,不需要请删除此行
 * @since 2015-6-18 下午2:46:17
 */
@Controller
@Scope("prototype")
public class ParkDeviceRestful
{
	private Log log = LogFactory.getLog(ParkDeviceRestful.class);
	@Autowired
	private ParkingDeviceService parkingDeviceService;

	//	[{"aid":"a1","gwid":"f0001","ukid": "a0101","tag":"ulock", "st":{ "on-off":0,"kwh":1, "car":0}}]
	@RequestMapping(value="/Device/DeviceComm.ashx")
	public void devicePost(HttpServletRequest request, HttpServletResponse response,String actionType)
	{
		//actionType=1 设备主动上报状态    actionType=1设备主动查询状态   
		String postData = RequestUtil.getPostRequestData(request);
		log.info("device post data="+postData);
		if ("1".equals(actionType)) {
			handlerPostStatus(postData,response);
		}else if ("2".equals(actionType)) {
			handlerSearchStatus(postData,response);
		}else {
			log.warn("设备上传接口actionType标记有误");
		}
	}

	/**
	 * 
	 * @author max.zheng
	 * @create 2015-6-20上午10:26:09
	 * @modified by
	 * @param postData
	 * @param response
	 */
	private void handlerSearchStatus(String postData,
			HttpServletResponse response) {
		//{"tag":"ulock","aid":"a1","gwid":"f0001","ask":"all"}   //查询该DTU下挂的全部设备
		ParkDeviceInfo parkDeviceInfo = (ParkDeviceInfo) JsonUtil.getDtoFromJsonObjStr(postData, ParkDeviceInfo.class);
	
		if (StringUtils.isNotBlank(parkDeviceInfo.getAsk())) {
			List<DeviceInfo> deviceInfos = new ArrayList<DeviceInfo>();
			if ("all".endsWith(parkDeviceInfo.getAsk())) {
//				{"tag":"ulock","aid":"a1","gwid":"f0001","ask":"all"}   //查询该DTU下挂的全部设备
				deviceInfos = ParkRedis.getDeviceInfosByArea(parkDeviceInfo.getAid());
			}else {
//				{"tag":"ulock","aid":"a1","gwid","f0001","ask":"123456"}   //查询该DTU下挂的单个 设备“123456”
				//单独查询某个设备 ask标记就是设备编码
				DeviceInfo deviceInfo = ParkRedis.getDeviceInfo(parkDeviceInfo.getAid(), parkDeviceInfo.getAsk());
				if (deviceInfo!=null) {
					deviceInfos.add(deviceInfo);
				}
				
			}
			
			if (EmptyUtil.isNotEmpty(deviceInfos)) {
				String returnData = DeviceInfo.getReturnStatus(deviceInfos);
				ResponseUtil.returnJsonpAsk(response, returnData);
			}else {
				ResponseUtil.returnJsonpAsk(response, "none");
				log.info("请求的设备编码不存在系统里,ukid="+parkDeviceInfo.getAsk());
			}
		}else {
			log.info("请求的设备编码为空,信息="+postData);
		}
		
	}

	/**
	 * /Device/DeviceComm.ashx?actionType=1
	 * @author max.zheng
	 * @create 2015-6-20上午10:25:50
	 * @modified by
	 * @param postData
	 * @param response
	 */
	private void handlerPostStatus(String postData, HttpServletResponse response) {
//		[{"aid":"a1","gwid":"f0001","ukid": "123456","tag":"ulock","st":{"onoff":0, "kwh":1, "car":0}}]
		//******降锁 无车
		//http://121.41.88.185:8080/Device/DeviceComm.ashx?actionType=1
		//[{"aid":"01","gwid":"f0001","ukid": "123457","tag":"ulock","st":{"onoff":1, "kwh":1, "car":0}}]
		
		//******升锁 有车
		//[{"aid":"01","gwid":"f0001","ukid": "123457","tag":"ulock","st":{"onoff":2, "kwh":1, "car":1}}]

		
		//json数据转对象
		List<ParkDeviceInfo> parkDeviceInfos = JsonUtil.getDTOList(postData, ParkDeviceInfo.class);
		if (EmptyUtil.isNotEmpty(parkDeviceInfos)) {
			//过滤判断是否有存在设备
			List<ParkDeviceInfo> checkList = parkingDeviceService.updateDevicePostInfo(parkDeviceInfos);
			if (EmptyUtil.isNotEmpty(checkList)) {
//				[{"tag":"ulock","aid":"a1","gwid":"f0001","ukid":"123456","error":1},{"tag":"ulock","aid":"a1","gwid":"f0001","ukid":"123457","error":1}]
				//未识别的设备列表错误返回
				String returnData = ParkDeviceInfo.returnUnknownDevice(parkDeviceInfos);
				ResponseUtil.returnJsonpAsk(response, returnData);
			}
		}
	}
	
	public static void main(String[] args) {
		//[{aid:"a1",gwid:"f0001",ukid:"123456",tag:"ulock",st:{onoff:0,kwh:1,car:0}},{aid:"a1",gwid:"f0001",ukid:"123457",tag:"ulock",st:{onoff:0,kwh:1,car:0}}]
//		String postData = "[{tag:\"ulock\", aid:\"a1\",gwid:\"f0001\",ukid: \"123456\",st:{onoff:0,kwh:1,car:0}},{tag:\"ulock\", aid:\"a1\",gwid:\"f0001\",ukid: \"123457\",st:{onoff:0,kwh:1,car:0}}]";
		//{tag:"ulock",aid:"a1",gwid:"f0001",ukid:"123456",error:1}
//		String postData = "[{\"tag\":\"ulock\", \"aid\":\"a1\",\"gwid\":\"f0001\",\"ukid\": \"123456\",\"st\":{\"onoff\":0,\"kwh\":1,\"car\":0}}]";
		String postData = "{\"tag\":\"ulock\",\"aid\":\"a1\",\"gwid\":\"f0001\",\"ask\":\"all\"}";
		//{tag:"ulock",aid:"a1",gwid:"f0001",ukid:"123456",error:1}
//		List<ParkDeviceInfo> parkDeviceInfos = JsonUtil.getDTOList(postData, ParkDeviceInfo.class);
		ParkDeviceInfo parkDeviceInfo = (ParkDeviceInfo) JsonUtil.getDtoFromJsonObjStr(postData, ParkDeviceInfo.class);
		System.out.println(JsonUtil.getJsonStr(parkDeviceInfo));
		//		System.out.println(parkDeviceInfos.size());
//		[{"tag":"ulock","aid":"a1","gwid":"f0001","ukid":"a0101","set":1},
//		 {"tag":"ulock","aid":"a1","gwid":"f0001","ukid":"a0101","set":0}, 
//		 {"tag":"ulock","aid":"a1","gwid":"f0001","ukid":"a0101","set":1}]
//		[{aid:"a1",gwid:"f0001",ukid:"123456",tag:"ulock",st:{onoff:0,kwh:1,car:0}},{aid:"a1",gwid:"f0001",ukid:"123457",tag:"ulock",st:{onoff:0,kwh:1,car:0}}]

//		{tag:"ulock",aid:"a1",gwid:"f0001",ukid:"123456",error:1}
//		System.out.println(JsonUtil.getJsonStr(parkDeviceInfos));
		
		
		//锁上传无车 关闭
//		[{"aid":"01","gwid":"f0001","ukid": "123456","tag":"ulock","st":{"onoff":2, "kwh":1, "car":0}}]
	}
}
